package com.itheima.io.inputstream.demo02;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 使用InputStream一次读取一个字节数组
 */
public class InputStreamDemo01 {

    public static void main(String[] args) throws IOException {

        // 1. 以多态的方式去创建一个InputStream对象
        InputStream inputStream = new FileInputStream("itheima-io-inputstream\\b.txt") ;

        // 2. 读取数据
        // public int read(byte[] b)一次读取一个字节数组,返回值代表的是读取到的有效字节个数,如果读取到了文件末尾返回的就是-1
        byte[] bytes = new byte[5] ;
//        int read = inputStream.read(bytes);
//
//        while(read != -1) {
//            System.out.print(new String(bytes , 0 , read));
//            read = inputStream.read(bytes);
//        }

        int len = -1 ;                  // 作用: 记录读取到的有效的字节个数
        while(  (len = inputStream.read(bytes)) != -1 ) {
            System.out.print(new String(bytes , 0 , len));
        }


        // System.out.println(read);               // 5
        // 把一个字节数组转换成字符串? 使用String类的构造方法
//        System.out.print(new String(bytes , 0 , read));

//        read = inputStream.read(bytes);
        // System.out.println(read);
//        System.out.print(new String(bytes , 0 , read));

//        read = inputStream.read(bytes);
        // System.out.println(read);
//        System.out.print(new String(bytes , 0 , read));

//        read = inputStream.read(bytes);
//        System.out.println(read);

        // 3. 释放资源
        inputStream.close();

    }

}
